package com.llkj.qingnianguoxue.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.SystemMessageListBean;
import com.llkj.qingnianguoxue.customview.SwipeLayout;
import com.llkj.qingnianguoxue.util.ClickViewCallback;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.StringUtils;

import java.util.List;

/**
 * 系统消息
 */
public class SystemMessageAdapter extends BaseQuickAdapter<SystemMessageListBean.SystemMessageBean, BaseViewHolder> {
    private ClickViewCallback listener;

    public SystemMessageAdapter(int layoutResId, List<SystemMessageListBean.SystemMessageBean> data, ClickViewCallback l) {
        super(layoutResId, data);
        listener = l;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, SystemMessageListBean.SystemMessageBean item) {

        setView(baseViewHolder, item);
    }

    private void setView(BaseViewHolder baseViewHolder, SystemMessageListBean.SystemMessageBean item) {
        FrameLayout rootView = baseViewHolder.getView(R.id.fl_message_item);

        rootView.removeAllViews();
        View itemView = null;

        TextView tvTime = null, tvContent = null,tvNewMsg=null;
        if (!"1".equals(item.getType()) && !"9".equals(item.getType())) {
            itemView = LayoutInflater.from(mContext).inflate(R.layout.item_system_message1, null);
        } else {
            itemView = LayoutInflater.from(mContext).inflate(R.layout.item_system_message2, null);
            ImageView ivPic = (ImageView) itemView.findViewById(R.id.iv_msg);
            if(!StringUtils.isEmpty(item.getUserpic())){
                ImageLoadingUtil.loadingImg(ivPic, item.getUserpic(),0);
            }else{
                ImageLoadingUtil.loadImg(ivPic,R.drawable.icon_message_system);
            }

            TextView tvAccept = (TextView) itemView.findViewById(R.id.tv_accept);
            TextView tvRefuse = (TextView) itemView.findViewById(R.id.tv_refuse);
            if("2".equals(item.getState())){// 1 待确认 2 已同意 3 已拒绝
                tvRefuse.setVisibility(View.GONE);
                tvAccept.setText("已同意");
                tvAccept.setSelected(true);
            }else if("3".equals(item.getState())){
                tvRefuse.setVisibility(View.GONE);
                tvAccept.setText("已拒绝");
                tvAccept.setSelected(true);
            }else{
                tvAccept.setVisibility(View.VISIBLE);
                tvRefuse.setVisibility(View.VISIBLE);
                baseViewHolder.addOnClickListener(R.id.tv_accept);
                baseViewHolder.addOnClickListener(R.id.tv_refuse);
            }
        }
        rootView.addView(itemView);
        setViewListener(itemView, baseViewHolder.getLayoutPosition(), 3);

        tvNewMsg = (TextView) itemView.findViewById(R.id.tv_new_msg);
        tvTime = (TextView) itemView.findViewById(R.id.tv_msg_time);
        tvTime.setText(item.getCreate_time());

        tvContent = (TextView) itemView.findViewById(R.id.tv_msg_content);
        tvContent.setText(item.getTitle());
        if("1".equals(item.getIs_read())){//已读
            tvNewMsg.setVisibility(View.INVISIBLE);
        }else{//未读
            tvNewMsg.setVisibility(View.VISIBLE);
        }

        SwipeLayout swipeDelGroup = baseViewHolder.getView(R.id.swipeDelGroup);
        TextView tvDelItem = baseViewHolder.getView(R.id.tvDelItem);

        baseViewHolder.addOnClickListener(R.id.tvDelItem);
        swipeDelGroup.close(true);

    }

    private void setViewListener(View v, final int position, final int type) {
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.clickViewCallback(position, type, v);
                }
            }
        });
    }
}
